package com.borya.dao.impl;

import com.borya.dao.RegisterNumConfigDAO;
import com.borya.framework.annotation.Component;
import com.borya.framework.core.ServerSqlSession;
import com.borya.model.db.RegisterNumConfig;
import org.apache.log4j.Logger;

/**
 * 描述:
 * 商户证件号可激活配置
 *
 * @Author: CYH
 * @Date: 2020/4/9 10:58
 */
@Component("registerNumConfigDAO")
public class RegisterNumConfigDAOImpl extends BaseDAOImpl implements RegisterNumConfigDAO {

    private Logger log = Logger.getLogger(getClass());

    @Override
    public RegisterNumConfig getNumByType(Integer merchantType, int detailType, Integer type) {
        StringBuilder sql = new StringBuilder(256);
        sql.append("SELECT * FROM tb_register_num_config_").append(detailType);
        sql.append(" WHERE merchant_type=? and type=? limit 1");
        try (ServerSqlSession session = getSqlDataSource().session()) {
            return session.executeQuery(sql.toString(), rs -> {
                if (rs != null && rs.next()) {
                    RegisterNumConfig config = new RegisterNumConfig();
                    config.setDetailType(rs.getInt("detail_type"));
                    config.setMerchantType(rs.getInt("merchant_type"));
                    config.setType(rs.getInt("type"));
                    config.setMaxRegisterNum(rs.getInt("max_register_num"));
                    return config;
                }
                log.warn("DB not found config,merchantType=" + merchantType + " ,type=" + type);
                return null;
            }, merchantType, type);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return null;
        }
    }
}
